#include "stdio.h"
#include "SDL/SDL.h"

int main(int argc, char *argv[]) {
SDL_Surface *screen; //This pointer will reference the backbuffer
SDL_Surface *image; //This pointer will reference our bitmap sprite
SDL_Rect src, dest;//These rectangles will describe the source and destination regions ofour blit
SDL_Rect hrc, desti;
SDL_Rect hrc1, desti1;
SDL_Rect hrc2, desti2;
SDL_Rect hrc3, desti3;
SDL_Event event;
SDL_Surface *im;  //This pointeur will reference to helico.bmp
SDL_Surface *im1; //This pointeur will reference to left.bmp
SDL_Surface *im2; //This pointeur will reference to up.bmp
SDL_Surface *im3; //This pointeur will reference to down.bmp
//We must first initialize the SDL video component, and check for success
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
//Set the video mode to fullscreen 640x480 with 16bit colour and double- buffering
screen = SDL_SetVideoMode(798,601, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
//Load the bitmap into a temporary surface, and check for success
image = SDL_LoadBMP("img/image.bmp");
if (image == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
im = SDL_LoadBMP("img/helico.bmp");
if (im == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
im1= SDL_LoadBMP("img/left.bmp");
if (im1== NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
im2= SDL_LoadBMP("img/up.bmp");
if (im2== NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
im3 = SDL_LoadBMP("img/down.bmp");
if (im3 == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}

SDL_SetColorKey(im, SDL_SRCCOLORKEY, SDL_MapRGB(im->format, 255, 0, 255));
SDL_SetColorKey(im3, SDL_SRCCOLORKEY, SDL_MapRGB(im3->format, 255, 0, 255));
SDL_SetColorKey(im1, SDL_SRCCOLORKEY, SDL_MapRGB(im1->format, 255, 255, 255));
SDL_SetColorKey(im2, SDL_SRCCOLORKEY, SDL_MapRGB(im2->format, 255, 255, 255));
//Convert the surface to the appropriate display format
//image = SDL_DisplayFormat(image);
//Release the temporary surface
//SDL_FreeSurface(image);
//Construct the source rectangle for our blit
src.x = 0;
src.y = 0;
src.w = image->w; //Use image->w to display the entire width of the image
src.h = image->h; //Use image->h to display the entire height of the image
hrc.x =0;
hrc.y =0;
hrc.w =im->w;
hrc.h =im->h;
hrc1.x =0;
hrc1.y =0;
hrc1.w =NULL;
hrc1.h =NULL;
hrc2.x =0;
hrc2.y =0;
hrc2.w =NULL;
hrc2.h =NULL;
hrc3.x =0;
hrc3.y =0;
hrc3.w =NULL;
hrc3.h =NULL;
//Construct the destination rectangle for our blit
int done=1;
dest.x = 0;//Display the image at the (X,Y) coordinates (100,100)
dest.y = 0;
dest.w = image->w;
dest.h = image->h;
desti.x = 100;
desti.y = 350;
desti.w = im->w;
desti.h = im->h;
desti1.x = 100;
desti1.y = 350;
desti1.w = im1->w;
desti1.h = im1->h;
desti2.x = 100;
desti2.y = 350;
desti2.w = im2->w;
desti2.h = im2->h;
desti3.x = 100;
desti3.y = 350;
desti3.w = im3->w;
desti3.h = im3->h;
SDL_EnableKeyRepeat(10,10);
while(done){
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = 0;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = 0;
switch(event.key.keysym.sym)
        {
            case SDLK_UP: // Flèche haut
                desti2.y-=5;
                desti.y=desti2.y;
                desti1.y=desti2.y;
                desti3.y=desti2.y;
               hrc2.w =im2->w;
               hrc2.h =im2->h;
                hrc.w =NULL;
                hrc.h =NULL;
                 hrc1.w =NULL;
                hrc1.h =NULL;
                 hrc3.w =NULL;
                hrc3.h =NULL;



                break;
            case SDLK_DOWN: // Flèche bas
                if((desti3.y+im3->h)<(screen->h))
                desti3.y+=5;
                desti2.y=desti3.y;
                 desti.y=desti3.y;
                  desti1.y=desti3.y;
                hrc3.w =im3->w;
                hrc3.h =im3->h;
                 hrc1.w =NULL;
                hrc1.h =NULL;
                 hrc.w =NULL;
                hrc.h =NULL;
                 hrc2.w =NULL;
                hrc2.h =NULL;

                break;
            case SDLK_RIGHT: // Flèche droite
               if((desti.x +im->w)<(screen->w))
                desti.x+=5;
                desti2.x=desti.x;
                desti1.x=desti.x;
                desti3.x=desti.x;

                hrc.w =im->w;
                hrc.h =im->h;
                 hrc1.w =NULL;
                hrc1.h =NULL;
                 hrc2.w =NULL;
                hrc2.h =NULL;
                 hrc3.w =NULL;
                hrc3.h =NULL;

                break;
            case SDLK_LEFT: // Flèche gauche
                desti1.x-=5;
                desti2.x=desti1.x;
                desti.x=desti1.x;
                desti3.x=desti1.x;
                hrc1.w =im1->w;
                hrc1.h =im1->h;
                 hrc2.w =NULL;
                hrc2.h =NULL;
                 hrc3.w =NULL;
                hrc3.h =NULL;
                 hrc.w =NULL;
                hrc.h =NULL;

                break;
        }

break;
}
} // end switch
} // end of message processing
//Blit the image to the backbuffer
SDL_BlitSurface(image, &src, screen, &dest);
SDL_BlitSurface(im, &hrc, screen, &desti);
SDL_BlitSurface(im1,&hrc1, screen, &desti1);
SDL_BlitSurface(im2, &hrc2, screen, &desti2);
SDL_BlitSurface(im3, &hrc3, screen, &desti3);
//Flip the backbuffer to the primary Hardware Video Memory
SDL_Flip(screen);}
//Release the surface
SDL_FreeSurface(image);
SDL_FreeSurface(im);
SDL_FreeSurface(im1);
SDL_FreeSurface(im2);
SDL_FreeSurface(im3);
return 0; //Return success!
}
